package com.yunkaweilai.android.fragment.member;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.member.MemberRechargeActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.base.BaseFragment;
import com.yunkaweilai.android.utils.MoneyTextWatcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 随意充值
 * Created by Administrator on 2017/6/28.
 */

public class FreeRechargeFragment extends BaseFragment {
    Unbinder unbinder;
    public static final String RECHARGE_TYPE = "RECHARGE_TYPE";
    @BindView(R.id.id_edt_recharge_money)
    EditText idEdtRechargeMoney;
    @BindView(R.id.id_edt_give_money)
    EditText idEdtGiveMoney;


    @Override
    public int getContentViewId() {
        return R.layout.fragment_free_recharge;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        idEdtGiveMoney.setSelection(idEdtGiveMoney.length());
        idEdtRechargeMoney.setSelection(idEdtRechargeMoney.length());
        if (!(BaseApplication.getPayModel().getData().getInfo().getIs_edit_free_price() == null) && BaseApplication.getPayModel().getData().getInfo().getIs_edit_free_price().equals("1")) {
            idEdtGiveMoney.setFocusable(true);
        } else {
            idEdtGiveMoney.setFocusable(false);
            idEdtGiveMoney.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showToast("无修改赠送金额权限");
                }
            });
        }
        initClick();
    }

    private void initClick() {
        idEdtRechargeMoney.addTextChangedListener(new MoneyTextWatcher(idEdtRechargeMoney));
        idEdtGiveMoney.addTextChangedListener(new MoneyTextWatcher(idEdtGiveMoney));
    }


    @OnClick(R.id.id_btn_confirm)
    public void onViewClicked() {
        if (StringUtils.isEmpty(idEdtRechargeMoney.getText().toString()) || StringUtils.isEmpty(idEdtGiveMoney.getText().toString())) {
            showToast("还未填写金额");
        } else {
            Intent intent = new Intent();
            intent.putExtra(MemberRechargeActivity.PAY_TYPE_ID, "0");
            intent.putExtra(MemberRechargeActivity.PAY_TYPE_MONEY, idEdtRechargeMoney.getText().toString());
            intent.putExtra(MemberRechargeActivity.PAY_TYPE_GIVE_MONEY, idEdtGiveMoney.getText().toString());
            intent.putExtra(MemberRechargeActivity.PAY_TYPE_NAME, "随意充值");
            intent.putExtra(MemberRechargeActivity.PAY_TYPE_RULE, "");
            intent.putExtra(MemberRechargeActivity.PAY_TYPE_IS_TRUE, true);
            double i = Double.parseDouble(idEdtRechargeMoney.getText().toString());
            double b = Double.parseDouble(idEdtGiveMoney.getText().toString());
            intent.putExtra(MemberRechargeActivity.PAY_TYPE_ALL_MONEY, i + b + "");
            getActivity().setResult(Activity.RESULT_OK, intent);
            getActivity().finish();
        }
    }
}
